import java.util.Random;
import java.util.concurrent.CountDownLatch;

public class ThreadDemo35 {
    public static void main(String[] args) throws InterruptedException {
        //1，此处参数的意义就是有多少个线程任务
        CountDownLatch latch = new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {
            int id = i;
            Thread t = new Thread(()->{
                Random random = new Random();
                //随机生成一个1000到5000的数
                int time = (random.nextInt(5) + 1) * 1000;
                System.out.println("线程 " + id +"开始下载");
                try {
                    Thread.sleep(time);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("线程 " + id +"结束下载");
                //2，告知countDownLatch线程全部执行结束了
                latch.countDown();
            });
            t.start();
        }
        //3，通过await操作来等待所有线程任务结束，也就是countDown被调用了10次了
        latch.await();
        System.out.println("所有任务已经完成！");
    }
}
